
webix.ready(function(){
webix.ui.fullScreen();
 
var my_template={
    template: "<span style='font-size:20px; font-weight:bold; color:#3498DB'; >机台管理</span>&nbsp;/&nbsp;&nbsp; MachineManager",
    height:45,
    borderless:true 
  };	
	 var my_table={
        view:"datatable",
        id:"mydatatable",     
        columns:[
          {id:"machine_id",header:"机台内部编号",fillspace:true},
          {id:"brand",header:"机台品牌",fillspace:true,sort:"string",editor:"text"},
          {id:"model",header:"机台型号",fillspace:true,sort:"string",editor:"text"},
          {id:"purpose",header:"机台用途",fillspace:true,sort:"string",editor:"text"},
          {id:"machine_code",header:"客户编码",fillspace:true,sort:"string",editor:"text"},
          //删除
          { id:"",
            template:"<a href='#' onclick=\"delRow('#machine_id#')\">删除</a>",
            css:"padding_less",
            width:120
          }
        ],
        height:600,      
        select:true,
        // url:"http://192.168.1.111:9001/Application/demo"
      };

      var my_toolbar={
        view:"toolbar",
		id:"mytoolbar",
		
        elements:[           
          {
            view:"text",
            id:"s1",
			name:"s1",
            label:"客户编码:",
            width:300
          },
          {
              view:"text",
              id:"s2",
			  name:"s2",
              label:"机台品牌:",
              width:300
            },    {
                view:"text",
                id:"s3",
				name:"s3",
                label:"机台型号:",
                width:300
              },
          {view:"button",value:"查询",width:50,click:"select",css:"btn_check"},
          {view:"button",value:"添加机台",width:80,click:"addRow",css:"btn_check"},
          {view:"button",value:"确认添加",width:80,click:"confirm()",css:"btn_check"},
		  {view:"button",
	            template:"<form class='am-form' id='myform' action='upload' method='post' enctype='multipart/form-data'>" +
		        "<input style='margin-left: 4px; width: 165px; height: 32px;' name='myfile' id='upload' accept='application/vnd.ms-excel' type='file' style='float:left;'> " +
		        "<input class='webixtype_base'style='margin-left: 4px; width: 80px; height: 32px;'  id='search_sub' type='button' onclick='submitExcel()' value='确认上传'> " +
		        "</form>",
	            css:"btn_check",
	            width:265
	       },
          {view:"button",value:"导出模板",width:80,click:"download()",css:"btn_check"},
        ]   
      };
    var page={
            view:"pager",
            id:"pagerA",
           //新用户刚开始没有产生数据出现分页标签
		   // count:1000,
			//master:false,
			//group:15,
			//size:5,
			//page:1,
            template:"{common.first()}{common.prev()}{common.pages()}{common.next()}{common.last()}",
            css:"pager",
            on: {
                  onItemClick: function(id, e, node){
                   if (id == "next") {
                    if($$("pagerA").config.limit==$$("pagerA").config.page+1)
                      {
                        id = $$("pagerA").config.limit-1;
                      }
                      else
                      {
                        id=$$("pagerA").config.page+1;
                      }
                    }
                  else if(id=="prev")
                  {
                    id=$$("pagerA").config.page-1; 
                  }
                  else if(id=="first")
                  {
                    id=0;
                  }
                  else if(id=="last")
                  {
                    id=$$("pagerA").config.limit-1;
                  } 
                  var params={};
                  params.currPage =parseInt(id)+1;
                  params.pageSize =10;
                  postData("getMachineList", params,callback);
                }
            },
            height:38,
            css:{"text-align": "center","margin-top":"20px!important"}
        };
    var params={};
    params.pageSize =10;
    postData("getMachineList", params,callback);
  var web={
    container:"body",
    type:"line",
    id:"mylayout",
    rows:[
        my_template,
        my_toolbar,
        my_table,
        page
    ]
  };
  webix.ui(web);//---webix.ui结束----

  $$("s1").attachEvent("onTimedKeyPress", function(){
	var value1 = this.getValue();
	var name2=$$("s2").getValue();
	var name3=$$("s3").getValue();
   if(value1==""&&name2==""&&name3==""){
    location.reload();
    }
  });  
$$("s2").attachEvent("onTimedKeyPress", function(){
	var value2 = this.getValue();
	var name1=$$("s1").getValue();
	var name3=$$("s3").getValue();
   if(value2==""&&name1==""&&name3==""){
    location.reload();
    }
  });  
  $$("s3").attachEvent("onTimedKeyPress", function(){
	var value3 = this.getValue();
	var name2=$$("s2").getValue();
	var name1=$$("s1").getValue();
   if(value3==""&&name2==""&&name1==""){
    location.reload();
    }
  });  

})
function callback(data){
  var temp=JSON.parse(data); 
  var text1 = temp.data;
  // var data = new webix.DataCollection({data:text1.page});
  //$$('mydatatable').data.sync(data);
  $$("mydatatable").clearAll();
  $$("mydatatable").define("data",text1.page);
  $$('mydatatable').refresh();
  $$("pagerA").define("limit",text1.totalPageCount);
  $$("pagerA").define("count",text1.totalCount);
  $$("pagerA").define("size",text1.pageSize);
  $$("pagerA").define("page",text1.currPage-1);
  $$('pagerA').refresh();
}
var flag=true;
//行id
 var row1;
//添加一行
function addRow(){ 
 var dt=$$("mydatatable");
  if(flag){
   var id=dt.add({
   }); 
   row1=id;
   console.log(row1);
   dt.addRowCss(id, "newRow");
   dt.editRow(id);
    flag=false;
  }
 else{
   alert("请完成当前机台添加");
   console.log(row1);
   dt.editRow(row1);
 }
};
//获取表格值并刷新
function confirm(){
 var dt=$$("mydatatable");
 console.log(dt.config);
  dt.editStop();
  dt.eachRow(function(row){
    if(dt.hasCss(row,"newRow")==true){
      params=dt.getItem(row);
	  console.log(params);
      if(params.brand==""){
          alert("请填写机台品牌...");
           dt.editRow(row);
          return false;
        }
        if(params.model==""){
          alert("请填写机台型号...");
          dt.editRow(row);
          return false;
        }else if(params.purpose==""){
            alert("请填写机台用途...");
            dt.editRow(row);
            return false;
        }else if(params.machine_code==""){
        	 alert("请填写客户编号...");
             dt.editRow(row);
             return false;
        }else{
			flag=true;
			console.log(params);
        	 webix.ajax().post("addMachine", params, function(text, xml, xhr){
        		 var retData = JSON.parse(text);
             	if(retData.ret<0){
             		alert(retData.msg);
             		location.reload();
             	}else{
             		alert("执行完毕");
             		location.reload();
             	}
    	 	});
        }
      dt.removeRowCss(row, "newRow");
    }    
  });
}
function delRow(machine_id){
	 webix.confirm({
	        title:"提示",
	        text:"确定要删除吗?",
	        ok:"确定", 
	        cancel:"取消",
	        callback:function(result){
	        	if(result==true){
	        		var params = {};
	        		params.machine_id = machine_id;
	        		 webix.ajax().post("delMachine", params, function(text, xml, xhr){
	        				alert("执行完毕");
	        				location.reload();
	        		 });
	        	}
	        }
	        	
	        });
	 
}

//查询实现
function select(){
	  var params = {};
	  var machine_code=$$("s1").getValue();
	  var brand=$$("s2").getValue();
	  var model= $$("s3").getValue();
	  params.machine_code = machine_code;
	  params.brand = brand;
	  params.model = model;
	  if(params.machine_code==""&&params.brand==""&&params.model==""){
	    location.reload();
	  }
	  else{
	    postData("getMachineList",params,callback);
	  }
}
// 上传excel
function submitExcel(){
	
	var excelFile = $("#upload").val();
    if(excelFile=='') {
    	alert("请选择需上传的文件!");
    	return false;
    }
    if(excelFile.indexOf('.xls')==-1){
    	alert("文件格式不正确，请选择正确的Excel文件(后缀名.xls)！");
    	return false;
    }
    
    $("#search_sub").attr("disabled",true);
    $("#search_sub").css({"backgroundColor":"#BBBBBB"})
    $("#myform").submit();
    
}

// 导出模板
function download(){
	window.location.href ="download";
}